package com.hz.friends.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class InfosInputActivty extends BaseActivity {
	private TextView imbtn_bar_title;
	private EditText medittext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_infosinput);
		initViews();
		initEvents();
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initViews() {
		ImageButton imbtn_bar_left = (ImageButton) findViewById(R.id.imbtn_bar_left);
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right);
		Button btn_bar_righ = (Button) findViewById(R.id.btn_bar_righ);
		imbtn_bar_title = (TextView) findViewById(R.id.imbtn_bar_title);
		imbtn_bar_right.setVisibility(View.INVISIBLE);
		btn_bar_righ.setVisibility(View.VISIBLE);
		btn_bar_righ.setText("保存");
		imbtn_bar_left.setImageResource(R.drawable.ic_leftback_arrow);

		medittext = (EditText) findViewById(R.id.medittext);

		imbtn_bar_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btn_bar_righ.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
//				if (medittext.getText().toString().length() == 0) {
//					showToast("请输入");
//					return;
//				}
				Intent intent = new Intent();
				if(istextchange){
					intent.putExtra("text", medittext.getText().toString());
					intent.putExtra("ischange", true);
				}
				setResult(0, intent);
				finish();

			}
		});

		String titlemsg = getIntent().getStringExtra("TitleMessage");
		String text = getIntent().getStringExtra("text");
		imbtn_bar_title.setText(titlemsg);
		medittext.setText(text);
		Selection.setSelection((Spannable)medittext.getText(), text.length());
	}

	@Override
	protected void setViews() {

	}
      private boolean istextchange = false;
	@Override
	protected void initEvents() {
		medittext.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				istextchange = true;	
			}
		});
	}

}
